<template>
  <div class="create-team">
    <div class="header">
      <span class="back" @click="goBack">&lt;</span>
      <span class="title">创建新班组</span>
    </div>
    <form class="form">
      <div class="section">
        <div class="section-title">基础信息</div>
        <div class="form-item">
          <div class="label">班组名称 <span class="required">*</span></div>
          <input v-model="form.name" class="input" placeholder="请输入班组名称" />
          <div v-if="errors.name" class="error">{{ errors.name }}</div>
        </div>
        <div class="form-item">
          <div class="label">班组类型 <span class="required">*</span></div>
          <select v-model="form.type" class="input">
            <option value="">请选择班组介绍</option>
            <option value="维修班组">维修班组</option>
            <option value="保养班组">保养班组</option>
            <option value="安装班组">安装班组</option>
          </select>
          <div v-if="errors.type" class="error">{{ errors.type }}</div>
        </div>
        <div class="form-item">
          <div class="label">班组简介 <span class="required">*</span></div>
          <textarea v-model="form.brief" class="input textarea" placeholder="请输入班组介绍"></textarea>
          <div v-if="errors.brief" class="error">{{ errors.brief }}</div>
        </div>
      </div>
      <div class="section">
        <div class="section-title">附加信息</div>
        <div class="form-item">
          <div class="label">班组详情</div>
          <textarea v-model="form.detail" class="input textarea" placeholder="请输入班组详情（可选）"></textarea>
        </div>
      </div>
      <button class="submit-btn" type="button" @click="onSubmit">立即创建</button>
    </form>
    <div v-if="showSuccess" class="success-mask">
      <div class="success-dialog">
        <div class="success-msg">创建成功</div>
        <button class="success-close" @click="closeSuccess">关闭</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useTeamStore } from '../stores/TeamStatsStore.js'

const router = useRouter()
const teamStore = useTeamStore()
const form = ref({
  name: '',
  type: '',
  brief: '',
  detail: ''
})
const errors = ref({ name: '', type: '', brief: '' })
const showSuccess = ref(false)

function onSubmit() {
  errors.value = { name: '', type: '', brief: '' }
  let valid = true
  if (!form.value.name) {
    errors.value.name = '班组名称为必填项'
    valid = false
  } else if (teamStore.teams.some(t => t.name === form.value.name)) {
    errors.value.name = '班组名称已存在，请更换'
    valid = false
  }
  if (!form.value.type) {
    errors.value.type = '班组类型为必填项'
    valid = false
  }
  if (!form.value.brief) {
    errors.value.brief = '班组简介为必填项'
    valid = false
  }
  if (!valid) return
  // 添加到store（模拟数据库更新）
  teamStore.teams.push({
    id: Date.now(),
    name: form.value.name,
    type: form.value.type,
    leader: '未知',
    members: 1,
    createdAt: new Date().toISOString().slice(0,10),
    collected: false,
    joined: true,
    intro: form.value.brief,
    memberList: []
  })
  showSuccess.value = true
}

function closeSuccess() {
  showSuccess.value = false
  router.push('/main/team')
}

function goBack() {
  router.back()
}
</script>

<style scoped>
.create-team {
  background: #f7f8fa;
  min-height: 100vh;
  max-width: 375px;
  margin: 0 auto;
  box-sizing: border-box;
  padding-bottom: 24px;
}
.header {
  display: flex;
  align-items: center;
  height: 48px;
  background: #fff;
  font-size: 18px;
  font-weight: bold;
  border-bottom: 1px solid #f0f0f0;
  padding: 0 16px;
}
.back {
  font-size: 22px;
  margin-right: 12px;
  color: #333;
  cursor: pointer;
}
.title {
  flex: 1;
  text-align: left;
}
.form {
  margin: 18px 0 0 0;
}
.section {
  background: #fff;
  border-radius: 12px;
  margin: 0 16px 18px 16px;
  padding: 16px 14px 10px 14px;
}
.section-title {
  font-size: 15px;
  font-weight: bold;
  color: #222;
  margin-bottom: 10px;
}
.form-item {
  margin-bottom: 14px;
}
.label {
  font-size: 14px;
  color: #222;
  margin-bottom: 4px;
}
.required {
  color: #f56c6c;
  font-size: 15px;
}
.input {
  width: 100%;
  box-sizing: border-box;
  border: 1px solid #e5e6eb;
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 15px;
  background: #f7f8fa;
  margin-bottom: 0;
  outline: none;
}
.input:focus {
  border-color: #409eff;
  background: #fff;
}
.textarea {
  min-height: 60px;
  resize: none;
}
.tip {
  color: #f56c6c;
  font-size: 13px;
  margin-top: 2px;
}
.red {
  color: #f56c6c;
}
.submit-btn {
  width: 92%;
  margin: 18px 4% 0 4%;
  background: #409eff;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 13px 0;
  font-size: 17px;
  font-weight: 500;
  cursor: pointer;
  box-shadow: 0 2px 8px #e6eaf3;
}
.error {
  color: #f56c6c;
  font-size: 13px;
  margin-top: 2px;
}
.success-mask {
  position: fixed;
  left: 0; top: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.18);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
}
.success-dialog {
  background: #fff;
  border-radius: 10px;
  padding: 32px 36px 24px 36px;
  box-shadow: 0 2px 16px #e6eaf3;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.success-msg {
  font-size: 20px;
  color: #409eff;
  margin-bottom: 18px;
}
.success-close {
  background: #409eff;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 8px 32px;
  font-size: 16px;
  cursor: pointer;
}
</style> 